package com.qiaofang.applet.rest;

import com.qiaofang.applet.common.dto.DataResponse;
import com.qiaofang.applet.common.exception.BusinessException;
import com.qiaofang.applet.common.util.PageUtil;
import com.qiaofang.applet.constants.PropertyTradeTypeConstants;
import com.qiaofang.applet.constants.UpDownFlagConstants;
import com.qiaofang.applet.service.other.domain.CityCompany;
import com.qiaofang.applet.service.other.service.CityCompanyService;
import com.qiaofang.applet.service.property.domain.Property;
import com.qiaofang.applet.service.property.service.PropertyService;
import com.qiaofang.applet.service.property.service.dto.*;
import com.qiaofang.applet.service.property.service.mapstruct.PropertyMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * Created by IDEA.
 *
 * @auther Zym.
 * @company qiaofang
 * @date 2021/5/20 13:22
 */
@Slf4j
@RestController
@RequestMapping("/api/web/rockOpen")
@Api(tags = "磐石：开放接口")
public class RockOpenController {
    @Autowired
    private PropertyService propertyService;

    @Autowired
    private CityCompanyService cityCompanyService;

    @Autowired
    private PropertyMapper propertyMapper;

    @Value("${mpJumpUrl.property.saleList}")
    private String saleListJumpUrl;

    @Value("${mpJumpUrl.property.rentList}")
    private String rentListJumpUrl;

    @Value("${mpJumpUrl.property.saleDetail}")
    private String saleDetailJumpUrl;

    @Value("${mpJumpUrl.property.rentDetail}")
    private String rentDetailJumpUrl;


    @GetMapping("/searchProperty")
    @ApiOperation(value = "查询房源列表", notes = "查询房源列表")
    public DataResponse<RockPropertyResponse> searchSaleProperty(@RequestParam("communityId") Long estateRockId,
                                                                 @RequestParam("companyId") String companyUuid) {
        CityCompany cityCompany = cityCompanyService.getCompanyByUuid(companyUuid);
        if (Objects.isNull(cityCompany)){
            log.error("磐石小区调用巧房公司不存在：{}", companyUuid);
            throw new BusinessException("巧房公司不存在");
        }

        String corporationCode = cityCompany.getCorporationCode();
        String cityCode = cityCompany.getCityCode();

        RockPropertyResponse response = new  RockPropertyResponse();
        Integer saleTotal = 0;
        Integer rentTotal = 0;
        List<EstateGroupListDto> list = propertyService.getRockEstatePropertyNum(estateRockId, corporationCode, cityCode);
        if (!CollectionUtils.isEmpty(list)) {
            for (EstateGroupListDto estateGroupListDto : list) {
                String tradeType = estateGroupListDto.getTradeType();
                Integer total = estateGroupListDto.getTotal();
                if (PropertyTradeTypeConstants.ALL.equals(tradeType)) {
                    saleTotal += total;
                    rentTotal += total;
                } else if (PropertyTradeTypeConstants.SALE.equals(tradeType)) {
                    saleTotal += total;
                } else if (PropertyTradeTypeConstants.RENT.equals(tradeType)) {
                    rentTotal += total;
                }
            }
        }

        if (saleTotal > 0){
            SalePropertyQueryDto saleQueryDto = new SalePropertyQueryDto();
            saleQueryDto.setCorporationCode(corporationCode);
            saleQueryDto.setCityCode(cityCode);
            saleQueryDto.setEstateRockId(estateRockId);
            saleQueryDto.setUpDownFlag(UpDownFlagConstants.UP_FLAG.getValue());
            Page<Property> propertyPage = propertyService.findAllSaleByParams(saleQueryDto, PageRequest.of(0, 3));
            if (propertyPage.getTotalElements() > 0){
                List<Property> content = propertyPage.getContent();
                List<RockPropertyDto> saleRockList = new ArrayList<>(content.size());
                content.forEach(property -> {
                    RockPropertyDto rocDto = propertyMapper.domainToRockDetailDto(property);
                    rocDto.setDetailJumpUrl(saleDetailJumpUrl.replaceFirst("propertyUniqueUuid", property.getPropertyUniqueUuid()));
                    saleRockList.add(rocDto);
                });
                response.setSaleList(saleRockList);
            }
        }
        if (rentTotal > 0){
            RentPropertyQueryDto rentQueryDto = new RentPropertyQueryDto();
            rentQueryDto.setCorporationCode(corporationCode);
            rentQueryDto.setCityCode(cityCode);
            rentQueryDto.setEstateRockId(estateRockId);
            rentQueryDto.setUpDownFlag(UpDownFlagConstants.UP_FLAG.getValue());
            Page<Property> propertyPage = propertyService.findAllRentByParams(rentQueryDto, PageRequest.of(0, 3));
            if (propertyPage.getTotalElements() > 0){
                List<Property> content = propertyPage.getContent();
                List<RockPropertyDto> rentRockList = new ArrayList<>(content.size());
                content.forEach(property -> {
                    RockPropertyDto rocDto = propertyMapper.domainToRockDetailDto(property);
                    rocDto.setDetailJumpUrl(rentDetailJumpUrl.replaceFirst("propertyUniqueUuid", property.getPropertyUniqueUuid()));
                    rentRockList.add(rocDto);
                });
                response.setRentList(rentRockList);
            }
        }
        response.setSaleListJumpUrl(saleListJumpUrl.replaceFirst("panshiId", estateRockId + ""));
        response.setRentListJumpUrl(rentListJumpUrl.replaceFirst("panshiId", estateRockId + ""));
        response.setSaleTotal(saleTotal);
        response.setRentTotal(rentTotal);
        return DataResponse.ok(response);
    }
}
